#region Copyright (c) 2003, newtelligence AG. All rights reserved.
/*
// Copyright (c) 2003, newtelligence AG. (http://www.newtelligence.com)
// Original BlogX Source Code: Copyright (c) 2003, Chris Anderson (http://simplegeek.com)
// All rights reserved.
//  
// Redistribution and use in source and binary forms, with or without modification, are permitted 
// provided that the following conditions are met: 
//  
// (1) Redistributions of source code must retain the above copyright notice, this list of 
// conditions and the following disclaimer. 
// (2) Redistributions in binary form must reproduce the above copyright notice, this list of 
// conditions and the following disclaimer in the documentation and/or other materials 
// provided with the distribution. 
// (3) Neither the name of the newtelligence AG nor the names of its contributors may be used 
// to endorse or promote products derived from this software without specific prior 
// written permission.
//      
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -------------------------------------------------------------------------
//
// Original BlogX source code (c) 2003 by Chris Anderson (http://simplegeek.com)
// 
// newtelligence is a registered trademark of newtelligence Aktiengesellschaft.
// 
// For portions of this software, the some additional copyright notices may apply 
// which can either be found in the license.txt file included in the source distribution
// or following this notice. 
//
*/
#endregion

using System;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Web.Mail;
using System.Text;
using System.Collections;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Web.Core;

namespace newtelligence.DasBlog.Web
{
    /// <summary>
    /// Summary description for CommentView.
    /// </summary>
    public class CommentViewBox : System.Web.UI.UserControl 
    {
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.Label Label2;
        protected System.Web.UI.WebControls.Label Label3;
        protected System.Web.UI.WebControls.Label Label4;
        protected System.Web.UI.WebControls.TextBox name;
        protected System.Web.UI.WebControls.TextBox email;
        protected System.Web.UI.WebControls.TextBox homepage;
        protected System.Web.UI.WebControls.Button add;
        protected System.Web.UI.WebControls.CheckBox rememberMe;
        protected System.Web.UI.HtmlControls.HtmlGenericControl content;
        protected System.Web.UI.WebControls.RegularExpressionValidator validatorREEmail;
        protected System.Web.UI.WebControls.RequiredFieldValidator validatorRFName;
        protected System.Web.UI.WebControls.RequiredFieldValidator validatorRFComment;
        protected System.Web.UI.HtmlControls.HtmlTable commentViewTable;
        protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
        protected System.Web.UI.HtmlControls.HtmlGenericControl commentViewContent;
        protected System.Web.UI.WebControls.TextBox comment;
        protected System.Web.UI.HtmlControls.HtmlForm CommentView;
		protected WebControlCaptcha.CaptchaControl CaptchaControl1;
		protected System.Web.UI.WebControls.Label labelCommentsClosed;
		protected System.Web.UI.HtmlControls.HtmlGenericControl commentsClosed;
        protected System.Resources.ResourceManager resmgr;

        public CommentViewBox()
        {
        }
        
        private void Page_Load(object sender, System.EventArgs e)
        {
            SharedBasePage requestPage = Page as SharedBasePage;
        
			// if you are commenting on your own blog, no need for Captha
			if (SiteSecurity.IsValidContributor())
			{
				CaptchaControl1.Enabled = CaptchaControl1.Visible = false;				
			}
			else
			{
				CaptchaControl1.Enabled = CaptchaControl1.Visible = requestPage.SiteConfig.EnableCaptcha;
			}

            resmgr = ApplicationResourceTable.Get();

            if (!IsPostBack)
            {
                if ( requestPage.WeblogEntryId.Length == 0 )
                {
                   requestPage.Redirect(Utils.GetStartPageUrl(requestPage.SiteConfig));
                }
                ViewState["entryId"] = requestPage.WeblogEntryId;

                if (Request.Cookies["email"] != null && Request.Cookies["name"] != null)
                {
                    email.Text = Request.Cookies["email"].Value;
					string nameStr = Request.Cookies["name"].Value;
					//truncate at 32 chars to avoid abuse...
                    name.Text = nameStr.Substring(0,Math.Min(32,nameStr.Length));
                    homepage.Text = Request.Cookies["homepage"].Value;
                }
            }
            DataBind();
        }

		#region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
		
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
			this.add.Click += new System.EventHandler(this.add_Click);
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.CommentView_PreRender);

		}
		#endregion

        private void CommentView_PreRender(object sender, System.EventArgs e)
        {
            SharedBasePage requestPage = Page as SharedBasePage;
            string entryId = (string)ViewState["entryId"];
            bool obfuscateEmail = SiteConfig.GetSiteConfig().ObfuscateEmail;

            Entry entry = requestPage.DataService.GetEntry(entryId);
			if ( entry != null )
			{
				//Modified 10-3-03 HPierson
				//Render the day template with just the single entry, rather than the item template
                //Modified 12-8-03 HPierson
                //Using entry.CreatedLocalTime causes a bug when dasBlog is not configured to be in 
                //the same time zone as the server. Instead, we use the configured WindowsTimeZone
                //to calculate the dasBlog configured local time for the entry
                requestPage.WeblogEntries.Add(entry);
				if (requestPage.SiteConfig.AdjustDisplayTimeZone)
				{
					newtelligence.DasBlog.Util.WindowsTimeZone wtz = requestPage.SiteConfig.GetConfiguredTimeZone();
					requestPage.ProcessDayTemplate(wtz.ToLocalTime(entry.CreatedUtc), commentViewContent);
				}
				else
				{
					requestPage.ProcessDayTemplate(entry.CreatedUtc, commentViewContent);
				}
			
				HtmlAnchor commentStart = new HtmlAnchor();
				commentStart.Name = "commentstart";
				commentViewContent.Controls.Add(commentStart);

				foreach (Comment c in requestPage.DataService.GetCommentsFor(entryId))
				{
					SingleCommentView view = (SingleCommentView)LoadControl("SingleCommentView.ascx");
					view.Comment = c;
					view.ObfuscateEmail = obfuscateEmail;
					commentViewContent.Controls.Add(view);
				}

				commentsClosed.Visible = false;
				commentViewTable.Visible = true;

				if (Utils.AreCommentsAllowed(entry, requestPage.SiteConfig) == false)
				{
					commentsClosed.Visible = true;
					commentViewTable.Visible = false;
				}

				if(Page.IsClientScriptBlockRegistered("coCommentScript") == false)
				{
					string coCommentScript = String.Format(@"
					<script type=""text/javascript"">

					var blogTool               = ""DasBlog"";
					var blogURL                = ""{0}"";
					var blogTitle              = ""{1}"";
					var postURL                = ""{2}"";
					var postTitle              = ""{3}"";
					var commentAuthorFieldName = ""{4}"";
					var commentAuthorLoggedIn  = false;
					var commentFormID          = ""mainForm"";
					var commentTextFieldName   = ""{5}"";
					var commentButtonName      = ""{6}"";
					</script>",
							requestPage.SiteConfig.Root,
							Server.HtmlEncode(requestPage.SiteConfig.Title),
							Request.Url.ToString(),
							Server.HtmlEncode(entry.Title),
							this.name.ClientID,
							this.comment.UniqueID,
							this.add.ClientID
						);
					Page.RegisterClientScriptBlock("coComment",coCommentScript);	
				}
				

			}
        }

        private string FixUrl( string url )
        {
            if ( url != null && url.Length > 0 )
            {
                if ( url.ToLower().StartsWith("http://"))
                {
                    return url;
                }
                else
                {
                    return "http://" + url;
                }
            }
            else
            {
                return url;
            }
        }

        private void add_Click(object sender, System.EventArgs e)
        {
			SharedBasePage requestPage = Page as SharedBasePage;

			if (CaptchaControl1.Enabled && requestPage.SiteConfig.EnableCaptcha == true)
			{
				if (CaptchaControl1.UserValidated == false) 
				{
					return;
				}
			}
			
            if ( Page.IsValid )
            {
                string entryId = ViewState["entryId"].ToString().ToUpper();

                if (rememberMe.Checked)
                {
					string path = HttpRuntime.AppDomainAppVirtualPath;
					
					//We don't encode the name so High Latin folks like Ren Drie�el don't break
					// the Http Input Validation stuff.
					HttpCookie cookieName = new HttpCookie("name",name.Text);
					cookieName.Path = path;
					Response.Cookies.Add(cookieName);

					HttpCookie cookieEmail = new HttpCookie("email", HttpUtility.HtmlEncode(email.Text));
					cookieEmail.Path = path;
                    Response.Cookies.Add(cookieEmail);
                    
					HttpCookie cookieHomepage = new HttpCookie("homepage", HttpUtility.HtmlEncode(homepage.Text));
					cookieHomepage.Path = path;
					Response.Cookies.Add(cookieHomepage);

					Response.Cookies["name"].Expires = DateTime.MaxValue;
					Response.Cookies["email"].Expires = DateTime.MaxValue;
					Response.Cookies["homepage"].Expires = DateTime.MaxValue;
                }

                Entry entry = requestPage.DataService.GetEntry(entryId);
                if (entry!=null)
                {
                    Comment c = new Comment();
                    c.Initialize();
                    c.Author = HttpUtility.HtmlEncode(name.Text);
                    c.AuthorEmail = HttpUtility.HtmlEncode(email.Text);
                    c.AuthorHomepage = FixUrl(homepage.Text);
                    c.AuthorIPAddress = Request.UserHostAddress;
                    c.Content = Server.HtmlEncode(comment.Text);
                    c.TargetEntryId = entryId;
                    c.TargetTitle = entry.Title;

                    if ( requestPage.SiteConfig.SendCommentsByEmail &&
                        requestPage.SiteConfig.SmtpServer != null &&
                        requestPage.SiteConfig.SmtpServer.Length > 0 )
                    {
						SendMailInfo defaultMailInfo = ComposeMail(c);
                        requestPage.DataService.AddComment(c, defaultMailInfo);
						requestPage.DataService.RunActions(ComposeMailForUsers(entry, c));
						
                        string commentShort = c.Content.Replace("\n","");
                        if ( commentShort.Length > 50 )
                        {
                            commentShort = commentShort.Substring(0,50)+"...";
                        }
                        requestPage.LoggingService.AddEvent(
                            new EventDataItem( 
                            EventCodes.CommentAdded, commentShort , Utils.GetCommentViewUrl(entryId)));
                    }
                    else
                    {
                        requestPage.DataService.AddComment(c);
                    }
                    
                    name.Text = "";
                    email.Text = "";
                    homepage.Text = "";
                    comment.Text = "";

					
					// break the caching
					HttpRuntime.Cache.Remove("BlogCoreData");

                    //Response.Redirect(Utils.GetStartPageUrl(requestPage.SiteConfig), false);

					//Send the user to the comment they JUST posted.
					Response.Redirect(Utils.GetCommentViewUrl(c.TargetEntryId) + "#" + c.EntryId);
                }
            }
        }
		private object[] ComposeMailForUsers(Entry entry, Comment c)
		{			
			ArrayList actions = new ArrayList();
						
			foreach(User user in SiteSecurity.GetSecurity().Users)
			{
				if (user.EmailAddress == null || user.EmailAddress.Length == 0)
					continue;

				if (user.NotifyOnAllComment || (user.NotifyOnOwnComment && entry.Author.ToUpper() == user.Name.ToUpper()))
				{					
					SendMailInfo sendMailInfo = ComposeMail(c);
					sendMailInfo.Message.To = user.EmailAddress;
					actions.Add(sendMailInfo);
				}
			}
						
			return (object[])actions.ToArray(typeof(object));
		}

		private SendMailInfo ComposeMail(Comment c)
		{
			SharedBasePage requestPage = Page as SharedBasePage;

			MailMessage emailMessage = new MailMessage();

			if (requestPage.SiteConfig.NotificationEMailAddress != null && 
				requestPage.SiteConfig.NotificationEMailAddress.Length > 0 )
			{
				emailMessage.To = requestPage.SiteConfig.NotificationEMailAddress;
			}
			else
			{
				emailMessage.To = requestPage.SiteConfig.Contact;
			}

			emailMessage.Subject = String.Format("Weblog comment by '{0}' from '{1}' on '{2}'", c.Author, c.AuthorHomepage, c.TargetTitle );
			emailMessage.Body = String.Format("{0}\r\nComments page: {1}",
				HttpUtility.HtmlDecode(c.Content), 
				Utils.GetCommentViewUrl(c.TargetEntryId));
			emailMessage.BodyFormat = MailFormat.Text;
			emailMessage.BodyEncoding = System.Text.Encoding.UTF8;
			if ( c.AuthorEmail != null && c.AuthorEmail.Length > 0 )
			{
				emailMessage.From = c.AuthorEmail;
			}
			else
			{
				emailMessage.From = requestPage.SiteConfig.Contact;
			}

			emailMessage.Headers.Add("Sender", requestPage.SiteConfig.Contact);
						
			// add the X-Originating-IP header
			string hostname = Dns.GetHostName();
			IPHostEntry ipHostEntry = Dns.GetHostByName(hostname);
						
			if (ipHostEntry.AddressList.Length > 0)
			{
				emailMessage.Headers.Add("X-Originating-IP", ipHostEntry.AddressList[0].ToString());
			}
			SendMailInfo sendMailInfo = new SendMailInfo(emailMessage, requestPage.SiteConfig.SmtpServer,
				requestPage.SiteConfig.EnableSmtpAuthentication, requestPage.SiteConfig.SmtpUserName, 
				requestPage.SiteConfig.SmtpPassword);

			return sendMailInfo;
		}
    }
}
